package com.fengye.webflux.mongo.controller;

import com.fengye.webflux.model.Article;
import com.fengye.webflux.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 *
 * @author sky
 * @date 2021年05月28日 3:02 下午
 */
@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("")
    public Flux<Article> getArticles() {
        return this.articleService.findAll();
    }


    @GetMapping("/{id}")
    public Mono<Article> getArticleById(@PathVariable String id) {
        return this.articleService.findOne(id);
    }

    @PostMapping("")
    public Mono<Article> createOrUpdateArticle(@RequestBody final Article article) {
        return this.articleService.save(article);
    }

    @DeleteMapping("/{id}")
    public Mono<Void> deleteArticle(@PathVariable final String id) {
        return this.articleService.delete(id);
    }

    @GetMapping("/author")
    public Flux<Article> findArticlesByAuthor(String author) {
        log.info("author = {}", author);
        return this.articleService.findByAuthor(author);
    }
}
